import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Platform, AlertController } from 'ionic-angular';
import { FileTransfer, FileTransferObject } from "@ionic-native/file-transfer";
import { File } from "@ionic-native/file";
import { FileOpener } from '@ionic-native/file-opener';
import { InAppBrowser } from '@ionic-native/in-app-browser';
import { Observable } from "rxjs/Observable";

/*
  Generated class for the UpgradeAppProvider provider.

  See https://angular.io/guide/dependency-injection for more info on providers
  and Angular DI.
*/
/*const APP_DOWNLOAD = 'http://omzo595hi.bkt.clouddn.com/download.html';//app下载地址(ios)
const APK_DOWNLOAD = 'http://omzo595hi.bkt.clouddn.com/ionic2_tabs.apk';//apk下载完整地址(android)*/

@Injectable()
export class UpgradeAppProvider {
  constructor(public http: HttpClient, private platform: Platform, private transfer: FileTransfer, private alertCtrl: AlertController, private file: File,
    private fileOpener: FileOpener, private inAppBrowser: InAppBrowser) {
    console.log('Hello UpgradeAppProvider Provider');
  }


  // websocket

  ws: WebSocket;
  we:WebSocket;
  wb:WebSocket;//弹窗接收消息
  wt:WebSocket;//新增学生
  createObservableSocket(url: string): Observable<any> {
    this.ws = new WebSocket(url);
    return new Observable(
      observer => {
        this.ws.onmessage = (event) => observer.next(event.data);
        this.ws.onerror = (event) => observer.error(event);
        this.ws.onclose = (event) => observer.complete();
      }
    )
  }

  sendMessage(msg: any) {
    this.ws.send(msg);
  }


  creatsocket(url: string): Observable<any> {
    this.we = new WebSocket(url);
    return new Observable(
      observer => {
        this.we.onmessage = (event) => observer.next(event.data);
        this.we.onerror = (event) => observer.error(event);
        this.we.onclose = (event) => observer.complete();
      }
    )
  }
  //弹幕
  creatsocketbarr(url: string): Observable<any> {
    this.wb = new WebSocket(url);
    return new Observable(
      observer => {
        this.wb.onmessage = (event) => observer.next(event.data);
        this.wb.onerror = (event) => observer.error(event);
        this.wb.onclose = (event) => observer.complete();
      }
    )
  }
//新增学生
  creatsocketstudent(url: string): Observable<any> {
    this.wt = new WebSocket(url);
    return new Observable(
      observer => {
        this.wt.onmessage = (event) => observer.next(event.data);
        this.wt.onerror = (event) => observer.error(event);
        this.wt.onclose = (event) => observer.complete();
      }
    )
  }





  /**
   * 下载安装app
   */
  downloadApp(appPath) {
    if (this.isAndroid()) {
      let alert = this.alertCtrl.create({
        title: '下载进度：0%',
        enableBackdropDismiss: false,
        buttons: ['后台下载']
      });
      alert.present();

      const fileTransfer: FileTransferObject = this.transfer.create();
      const apk = this.file.externalRootDirectory + 'android.apk'; //apk保存的目录

      fileTransfer.download(appPath, apk).then(() => {
        this.fileOpener.open(apk, 'application/vnd.android.package-archive');
      });

      fileTransfer.onProgress((event: ProgressEvent) => {
        let num = Math.floor(event.loaded / event.total * 100);
        if (num === 100) {
          alert.dismiss();
        } else {
          let title = document.getElementsByClassName('alert-title')[0];
          title && (title.innerHTML = '下载进度：' + num + '%');
        }
      });
    }
    if (this.isIos()) {
      this.openUrlByBrowser(appPath);
    }
  }

  /**
   * 通过浏览器打开url
   */
  openUrlByBrowser(url: string): void {
    this.inAppBrowser.create(url, '_system');
  }

  /**
   * 是否真机环境
   * @return {boolean}
   */
  isMobile(): boolean {
    return this.platform.is('mobile') && !this.platform.is('mobileweb');
  }

  /**
   * 是否android真机环境
   * @return {boolean}
   */
  isAndroid(): boolean {
    return this.isMobile() && this.platform.is('android');
  }

  /**
   * 是否ios真机环境
   * @return {boolean}
   */
  isIos(): boolean {
    return this.isMobile() && (this.platform.is('ios') || this.platform.is('ipad') || this.platform.is('iphone'));
  }




}
